package net.zjump.zzy.audiorecoder;

import android.Manifest;
import android.media.AudioRecord;
import android.media.MediaActionSound;
import android.media.MediaRecorder;
import android.os.Build;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import java.io.File;
import java.io.IOException;

public class MainActivity extends AppCompatActivity implements Handler.Callback {

    private MediaRecorder mediaRecorder;

    private boolean isRecoding;

    Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String path = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "a.aac";
        Log.i("zzy", "your path is :" + path);
        mediaRecorder = new MediaRecorder();
        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
        mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
        mediaRecorder.setOutputFile(path);
        mediaRecorder.setMaxDuration(60 * 1000 * 60);


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(new String[]{Manifest.permission.RECORD_AUDIO}, 1);
        }

        handler = new Handler(this);
    }

    private void getValue() {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                handler.sendEmptyMessage(1);
                getValue();
            }
        }, 1000);

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 1 && permissions[0].equals(Manifest.permission.RECORD_AUDIO)) {
            Log.i("zzy", "得到授权...");
        }
    }

    public void start(View view) {
        if (isRecoding) {
            return;
        }
        try {
            mediaRecorder.prepare();
            mediaRecorder.start();
            isRecoding = true;
            getValue();
            Log.i("zzy", "start record...");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void stop(View view) {
        if (!isRecoding) {
            return;
        }
        mediaRecorder.stop();
        mediaRecorder.reset();
        mediaRecorder.release();
        isRecoding = false;
        handler.removeMessages(1);
    }

    @Override
    public boolean handleMessage(Message message) {
        int ratio = mediaRecorder.getMaxAmplitude() / 600;
        int db = 0;
        if (ratio > 1) {
            db = (int) (20 * Math.log10(ratio));
        }
        Log.i("zzy", "value:" + db);
        return false;
    }
}
